﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class KhachHangDaiChung_KhachHangDaiChung_XemChiTietSanPham : System.Web.UI.Page
{
    string IdSP;
    static string back;
    string giacu;
    int soluong;
    string masp = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (Request["MaSP"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        if (!this.IsPostBack)
        {
            if(Request.UrlReferrer!=null)
                back = Request.UrlReferrer.ToString();
        }
        masp = Request["MaSP"].ToString();
        DataDSTableAdapters.SANPHAMTableAdapter spadapter = new DataDSTableAdapters.SANPHAMTableAdapter();
        DataDS.SANPHAMDataTable sptable = spadapter.SANPHAM_LayTheoMaSP(masp);
        DataDS.SANPHAMRow sprow = sptable[0];
        IdSP = sprow.Id.ToString();
        imgSanPham.ImageUrl = sprow.HinhAnh;
        lbTenSanPham.Text = sprow.TenSP;
        giacu = sprow.GiaCu.ToString();
        int maDVT = int.Parse(sprow.DonViTinh.ToString());
        DataDSTableAdapters.DONVITINHTableAdapter dvtAdapter = new DataDSTableAdapters.DONVITINHTableAdapter();
        DataDS.DONVITINHDataTable DVTtable = dvtAdapter.DONVITINH_LayTheoMa(maDVT);
        lbDonViTinh.Text = "/"+ DVTtable[0]["TenDonVi"].ToString();
        lbMoTa.Text = sprow.MoTa;
        lbNhaSanXuat.Text = sprow.NhaSanXuat;
        soluong = sprow.SoLuong;
        //site map
        SiteMapLoaiSanPham1.Mapl = sprow.Id_PhanLoai;
        SiteMapLoaiSanPham1.Url = "/SMO/ChucNang/NguoiDung/NguoiDung_XemDanhMucSanPham.aspx";
        //end sitemap
        if (sprow.SoLuong > 0)
            lbTrangThaiBan.Text = "Còn hàng. Số lượng: " + sprow.SoLuong.ToString();
        else
        {
            lbTrangThaiBan.Text = "Hết Hàng";
            //btnDuaVaoGioHang.Enabled = false;
        }
        if(sprow.ThoiGianDang < DateTime.Now.AddDays(-10) )
        {
            imgNew.Visible = false;
        }
        DataDSTableAdapters.SANPHAMYEUTHICHTableAdapter spytAdapter = new DataDSTableAdapters.SANPHAMYEUTHICHTableAdapter();
        if ((int)spytAdapter.SANPHAMYEUTHICH_SoLuongSanPhamTheoMaSP(masp) < int.Parse(ConfigurationSettings.AppSettings["NumToBeFavourite"]))
            imgYeuThich.Visible = false;


        //thuoc tinh
        DataDSTableAdapters.SANPHAM_THUOCTINHTableAdapter spttAdapter = new DataDSTableAdapters.SANPHAM_THUOCTINHTableAdapter();
        DataDS.SANPHAM_THUOCTINHDataTable spttTable = spttAdapter.SANPHAM_THUOCTINH_LayThuocTinhTheoMaSP(masp);
        string str = "";
        for (int i = 0; i < spttTable.Rows.Count; i++)
        {
            str += "<span>" + spttTable[i].TenThuocTinh + ": " + spttTable[i].GiaTri + "</span><br/><br/>";
        }
        lbThuocTinh.Text = str;
        //end thuoc tinh

        //khuyen mai
        DataDSTableAdapters.KHUYENMAI_CHITIETTableAdapter kmctadapter = new DataDSTableAdapters.KHUYENMAI_CHITIETTableAdapter();
        DataDS.KHUYENMAI_CHITIETDataTable kmcttable= kmctadapter.KHUYENMAI_LayChiTietTheoSP(int.Parse(IdSP));
        string skhuyenmai = "<table>";
        
        int giatri=0;
        if(kmcttable.Rows.Count >0)
        {
            DateTime beg = (DateTime)kmcttable.Rows[0]["NgayBatDau"];
            DateTime end = (DateTime)kmcttable.Rows[0]["NgayKetThuc"];
            string sbeg = beg.ToString("dd/M/yyyy");
            string send = end.ToString("dd/M/yyyy");
            skhuyenmai += "<tr>";
            skhuyenmai += "<td>Bắt đầu:" + sbeg + " <br/>Kết thúc:" + send + "</td></tr>";
            skhuyenmai += "<tr><td>" + kmcttable[0].NoiDung+ "</td></tr>";
        }
        lbGia.Text = giacu + " VND";
        for (int i = 0; i < kmcttable.Rows.Count; i++)
        {
            
            string tenhinhthuc = kmcttable.Rows[i]["TenHinhThuc"].ToString();
            int idhinhthuc = int.Parse(kmcttable.Rows[i]["Id_HinhThuc"].ToString());
            giatri = int.Parse(kmcttable.Rows[i]["GiaTri"].ToString());
            
            skhuyenmai += "<tr><td>";
            if (idhinhthuc == 2 && giatri > 0)
            {
                skhuyenmai += "<br/>" + tenhinhthuc + ":<b>" + giatri.ToString() + "</b>%";
                float giamoi = float.Parse(giacu) * (100 - giatri) / 100;
                lbGia.Text = "<strike>" + giacu + "VND </strike>" + " <b>" + giamoi.ToString() + " VND </b>";
            }
            else if (idhinhthuc == 1)
            {
                DataDS.SANPHAMDataTable spkmtable = spadapter.SANPHAM_LayTheoIdSP(giatri);
                skhuyenmai += "<br/>" + tenhinhthuc + ":<a href='NguoiDung_XemChiTietSanPham.aspx?MaSP=" + spkmtable.Rows[0]["MaSP"].ToString() + "' ><img width='50' height='50' src=\"" + spkmtable.Rows[0]["HinhAnh"].ToString().Replace("~", "/SMO") + "\" /></a>";
                skhuyenmai += spkmtable.Rows[0]["TenSP"] + ".Giá:<b>" + spkmtable.Rows[0]["GiaCu"].ToString() + " VND</b>";
            }
            skhuyenmai+="</td>";
            skhuyenmai += "</tr>";
            
        }
        skhuyenmai += "</table>";
        
        

        if (kmcttable.Rows.Count > 0)
            lblTangKem.Text = skhuyenmai;
       
                 
    }


    int kiemTraTonTaiSanPhamTrongGioHang(string IdSP)
    {
        DataTable table = (DataTable)Session["GioHang"];
        for (int i = 0; i < table.Rows.Count; i++)
        {
            DataRow row = table.Rows[i];
            if (row["Id_SP"].ToString() == IdSP)
                return i;
        }
        return -1;
    }
    protected void btnDuaVaoGioHang_Click(object sender, EventArgs e)
    {
        //kiem tra so luong san pham mua co vuot muc
       /* int soluongmua = int.Parse(txtSoLuong.Text);
        if (soluongmua > soluong)
        {
            Response.Write("<script>alert('Số lượng mua vượt quá số lượng hàng trong kho!')</script>");
            return;
        }*/
            
        // endkiem tra so luong san pham mua co vuot muc


        //int soluongmua = 0;
        //lblValidate.Text = "";
        //if (int.TryParse(txtSoLuong.Text,out soluongmua) == false)
        //{

        //    lblValidate.Text = "Số lượng không hợp lệ!";
        //    return;
        //}
       
        //if (soluongmua <= 0)
        //{
        //    lblValidate.Text = "Số lượng phải > 0!";
        //    return;
        //}
        //DataDSTableAdapters.KHUYENMAI_CHITIETTableAdapter kmctadapter = new DataDSTableAdapters.KHUYENMAI_CHITIETTableAdapter();
        //DataDS.KHUYENMAI_CHITIETDataTable kmcttable = kmctadapter.KHUYENMAI_LayChiTietTheoSP(int.Parse(IdSP));
        //int idhinhthuc=0; 
        //int giatri=0; 
        //if(kmcttable.Rows.Count>0)
        //{
        //    idhinhthuc= int.Parse(kmcttable.Rows[0]["Id_HinhThuc"].ToString());
        //    giatri = int.Parse(kmcttable.Rows[0]["GiaTri"].ToString());
        //}
        //DataTable table=(DataTable)Session["GioHang"];
        //DataRow row;
        //int kt=kiemTraTonTaiSanPhamTrongGioHang(IdSP);
        //if (kt == -1)
        //{
        //    if (soluongmua > soluong)
        //    {
        //        lblValidate.Text = "Số lượng vượt mức cho phép!";
        //        return;
        //    }
        //    row = table.NewRow();
        //    row["Id_HinhThuc"] = idhinhthuc;
        //    row["GiaTri"] = giatri;
        //    row["Id_SP"] = IdSP;
        //    row["SoLuong"] = txtSoLuong.Text;
        //    row["DonGia"] = giacu;
        //    if (int.Parse(row["Id_HinhThuc"].ToString()) == 2)
        //    {
        //        giatri = int.Parse(row["GiaTri"].ToString());
        //        row["TongTien"] = int.Parse(txtSoLuong.Text) * (float.Parse(row["DonGia"].ToString()) * (100 - giatri) / 100);
        //    }
        //    else
        //        row["TongTien"] = int.Parse(txtSoLuong.Text) * float.Parse(row["DonGia"].ToString());
        //    row["Is_KhuyenMai"] = "False";
        //    table.Rows.Add(row);
        //}
        //else
        //{
        //    row=table.Rows[kt];
        //    if (int.Parse(row["SoLuong"].ToString()) + soluongmua > soluong)
        //    {
        //        lblValidate.Text = "Số lượng vượt mức cho phép!";
        //        return;
        //    }
        //    row["SoLuong"] =Convert.ToString( int.Parse(row["SoLuong"].ToString()) + int.Parse(txtSoLuong.Text));
        //    if (int.Parse(row["Id_HinhThuc"].ToString()) == 2)
        //    {
        //        giatri = int.Parse(row["GiaTri"].ToString());
        //        row["TongTien"] = int.Parse(row["SoLuong"].ToString()) * (float.Parse(row["DonGia"].ToString()) * (100 - giatri) / 100);
        //    }
        //    else
        //        row["TongTien"] = int.Parse(row["SoLuong"].ToString()) * float.Parse(row["DonGia"].ToString());
        //   // table.Rows.Add(row);
        //   // table.Rows.RemoveAt(kt);
        //}
        ////lay san pham khuyen mai
        //if (idhinhthuc == 1)
        //{
        //    DataDSTableAdapters.SANPHAMTableAdapter spadapter = new DataDSTableAdapters.SANPHAMTableAdapter();
        //    DataDS.SANPHAMDataTable sptable= spadapter.SANPHAM_LayTheoIdSP(giatri);
        //    kt = kiemTraTonTaiSanPhamTrongGioHang(giatri.ToString());
        //    if (kt == -1)
        //    {
        //        row = table.NewRow();
        //        row["Id_HinhThuc"] = -1;
        //        row["GiaTri"] = 0;
        //        row["Id_SP"] = giatri;
        //        row["SoLuong"] = txtSoLuong.Text;
        //        row["DonGia"] = sptable.Rows[0]["GiaCu"].ToString();
        //        row["TongTien"] = int.Parse(txtSoLuong.Text) * float.Parse(row["DonGia"].ToString());
        //        row["Is_KhuyenMai"] = "True";
        //        table.Rows.Add(row);
        //    }
        //    else
        //    {
        //        row = table.Rows[kt];
        //        row["SoLuong"] = Convert.ToString(int.Parse(row["SoLuong"].ToString()) + int.Parse(txtSoLuong.Text));
        //        row["TongTien"] = int.Parse(row["SoLuong"].ToString()) * float.Parse(row["DonGia"].ToString());
        //        // table.Rows.Add(row);
        //        // table.Rows.RemoveAt(kt);
        //    }
        //}
        //Session["GioHang"] = table;
        
        ////hien thong bao
        //DataDSTableAdapters.SANPHAMTableAdapter sptbadapter = new DataDSTableAdapters.SANPHAMTableAdapter();
        //DataDS.SANPHAMDataTable sptbtable;
        //string thongbao = "<center><table><b>Thông tin giỏ hàng của bạn<b>";
        //thongbao += "<tr>" +
        //            "<th>Tên SP</th>" +
        //            "<th>Số Lượng</th>" +
        //            "<th>Đơn Giá</th>" +
        //            "<th>Thành Tiền</th>"+
        //            "<th>Khuyến mãi</th>" + 
        //            "</tr>";
        //table = (DataTable)Session["GioHang"];
        //float tong=0;
        //foreach (DataRow datarow in table.Rows)
        //{
        //    string id = datarow["Id_SP"].ToString();
        //    sptbtable = sptbadapter.SANPHAM_LayTheoIdSP(int.Parse(id));
        //    DataDS.SANPHAMRow sprow = sptbtable[0];
        //    thongbao += "<tr>";
        //    thongbao += "<td>" + sprow["TenSP"] + "</td>";
        //    thongbao += "<td>" + datarow["SoLuong"] + "</td>";
        //    thongbao += "<td>" + datarow["DonGia"] + "</td>";
        //    thongbao += "<td>" + datarow["TongTien"] + "</td>";
        //    if (datarow["Is_KhuyenMai"].ToString() == "True")
        //        thongbao += "<td><b> SP Khuyến mãi</b> </td>";
        //    else
        //    {
        //        if (int.Parse(datarow["Id_HinhThuc"].ToString()) == 2)
        //        {
        //            giatri = int.Parse(datarow["GiaTri"].ToString());
        //            thongbao += "<td><b>Giảm giá:" + giatri.ToString() + " %</b></td>";
        //        }
        //        else
        //        {
        //            if (int.Parse(datarow["Id_HinhThuc"].ToString()) == 1)
        //            {
        //                thongbao += "<td><b>Tặng kèm sản phẩm</b></td>";
        //            }
        //            else
        //                thongbao += "<td></td>";
        //        }
        //    }
        //    thongbao += "</tr>";
        //    if(datarow["Is_KhuyenMai"].ToString()=="False")
        //        tong+=float.Parse(datarow["TongTien"].ToString());
        //}
        //thongbao += "<tr><td colspan='5' align='right'>Tổng tiền:"+tong.ToString()+"</td></tr>";
        //thongbao += "</table></center>";
        //lblGioHang.Text = thongbao;
        //msg.Show();
        
        
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(back);
        //Response.Write("<script>history.go(-1)</script>");
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_CapNhatSanPhamChiTiet.aspx?MaSP=" + masp);
    }
   
}
